package com.fanjia.crm.web.security;

import com.fanjia.crm.mapper.PermissionMapper;
import com.fanjia.crm.model.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/**
 * Created by FanJia on 2020/9/20 11:04*/


@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    PermissionMapper permissionMapper;


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers()
                .frameOptions().sameOrigin()
                .httpStrictTransportSecurity().disable();
        http.csrf().disable();

        List<Permission> pmsList = permissionMapper.findAllPermission();
        //加载所有的权限数据
        for(Permission pms : pmsList){
            if(!StringUtils.isEmpty(pms.getPath())) {
                http.authorizeRequests().antMatchers(pms.getPath()).hasAnyAuthority(pms.getAuthorization_flag());
            }
        }


        http.authorizeRequests()
               //.antMatchers("/courseorder/list").hasAnyAuthority("COURSEORDER_READ")

                .antMatchers("/login").permitAll()
                .antMatchers("/**").fullyAuthenticated()
                .and()
                .formLogin()
                .loginPage("/login");
                //.failureUrl("/loginerror");

    }
}
